"""
Copyright (c) 2024 lowyyh
command-line-three-piece-chess is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
import os
import random
import super_computer


def out():
    for i in q:
        h = 0
        for i2 in i:
            h += 1
            try:
                print("  " + i2[0] + "  ", end='')
            except IndexError:
                print("     ", end='')
            if h == 3:
                print()
            else:
                print("|", end='')
        print("-----|-----|-----")


def pd(x, y):
    if not q[x][y]:
        return True
    else:
        return False


def play():
    while True:
        x_and_y = input("下棋坐标 > ")
        if " " in x_and_y:
            x, y = x_and_y.split(" ")
            try:
                x = int(x) - 1
                y = int(y) - 1
                if (x > 2 or y > 2) or not pd(x, y):
                    print("Error")
                else:
                    break
            except ValueError:
                print("Error")
        elif "," in x_and_y:
            x, y = x_and_y.split(",")
            try:
                x = int(x) - 1
                y = int(y) - 1
                if (x > 2 or y > 2) or not pd(x, y):
                    print("Error")
                else:
                    break
            except ValueError:
                print("Error")
    q[x][y] = "*"


def c_play():
    print("计算机下棋")
    while True:
        x = random.randint(0, 2)
        y = random.randint(0, 2)
        if pd(x, y):
            q[x][y] = "#"
            break


def all_win():
    for i in q:
        for ii in i:
            if not ii:
                return False
    return True


def win():
    # 横向判断
    for i in q:
        if i[0] == i[1] == i[2] == "*":
            return True
        elif i[0] == i[1] == i[2] == "#":
            return False
    # 竖向判断
    for i in range(3):
        if q[0][i] == q[1][i] == q[2][i] == "*":
            return True
        elif q[0][i] == q[1][i] == q[2][i] == "#":
            return False
    # 斜向判断
    if (q[0][0] == q[1][1] == q[2][2] == "*") or (q[0][2] == q[1][1] == q[2][0] == "*"):
        return True
    elif (q[0][0] == q[1][1] == q[2][2] == "#") or (q[0][2] == q[1][1] == q[2][0] == "#"):
        return False
    return None


if __name__ == '__main__':
    while True:
        n = 0
        q = [
            [[], [], []],
            [[], [], []],
            [[], [], []]
        ]
        play_game = True
        if input("play game?[y/n]") == "n":
            print("bye!!")
            exit(0)
        while play_game:
            for playing in [play, super_computer.mast_win]:
                out()
                if playing == play:
                    playing()
                else:
                    playing(q, n)
                    n = 1
                state = win()
                # print(state)
                if state:
                    out()
                    print("你赢了")
                    play_game = False
                    break
                elif state is None:
                    if all_win():
                        out()
                        print("平局")
                        play_game = False
                        break
                else:
                    out()
                    print("你输了")
                    play_game = False
                    break
